// pages/ReservationConfirmation/ResvaConfirmation.js
import exhibition from '../../api/exhibition';
import util from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    exhibitionData: {},
    columns: ['居民身份证',
      '护照',
      '港澳居民来往内地通行证',
      '台湾居民来往大陆通行证',
      '军官证',
      '警官证',
      '士兵证',
      '其他证件'
    ],
    show: false,
    userOrder: [{
      ticketId: '',
      username: '',
      certificate: '',
      identity: '',
      radio: '',
      cost: '',
      show: false
    }],
    totalCost: 0,
    ticket: [{
      radio: '1',
      discount: 1
    }, {
      radio: '2',
      discount: 0.8
    }, {
      radio: '3',
      discount: 0.5
    }, {
      radio: '4',
      discount: 0.5
    }, ],
  },

  onConfirm(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    const Index = event.currentTarget.dataset.index;
    const key = `userOrder[${Index}].certificate`;
    const showKey = `userOrder[${Index}].show`;
    this.setData({
      [key]: value,
      [showKey]: false,
      show: false
    })
  },

  onCancel(event) {
    const index = event.currentTarget.dataset.index;
    const showKey = `userOrder[${index}].show`;
    this.setData({
      [showKey]: false,
      show: false
    })
  },

  showPopup(event) {
    const index = event.currentTarget.dataset.index;
    const showKey = `userOrder[${index}].show`;
    this.setData({
      [showKey]: true,
      show: true
    });
  },

  onClose(event) {
    const index = event.currentTarget.dataset.index;
    const showKey = `userOrder[${index}].show`;
    this.setData({
      [showKey]: false,
      show: false
    });
  },
  onChange(event) {
    const index = event.currentTarget.dataset.index;
    for (let i = 0; i < this.data.ticket.length; i++) {
      if (this.data.ticket[i].radio == event.detail) {
        const key = `userOrder[${index}].cost`;
        const rKey = `userOrder[${index}].radio`;
        this.setData({
            [key]: this.data.exhibitionData.ticketPrice * this.data.ticket[i].discount
          },
          () => {
            this.calculateTotal();
          });
        this.setData({
          [rKey]: event.detail
        })
      }
    }
  },
  // 计算总价
  calculateTotal() {
    let total = 0;
    for (let j = 0; j < this.data.userOrder.length; j++) {
      total = this.data.userOrder[j].cost + total;
    }
    this.setData({
      totalCost: total.toFixed(2)
    });
  },
  // 添加观众
  addAudience() {
    const newAudience = {
      ticketId: '',
      username: '',
      certificate: '',
      identity: '',
      radio: '',
      cost: '',
      show: false
    };
    this.setData({
      userOrder: [...this.data.userOrder, newAudience]
    });
    console.log(this.data.userOrder)
  },
  // 更新姓名
  onNameChange(e) {
    const index = e.currentTarget.dataset.index;
    const value = e.detail;
    const key = `userOrder[${index}].username`;
    this.setData({
      [key]: value
    });
  },

  // 更新票种
  onTicketTypeChange(e) {
    const index = e.currentTarget.dataset.index;
    const value = e.detail;
    const key = `userOrder[${index}].radio`;
    this.setData({
      [key]: value
    });
  },
  //更新身份证
  onIdentityChange(e) {
    const index = e.currentTarget.dataset.index;
    const value = e.detail;
    const key = `userOrder[${index}].identity`;
    this.setData({
      [key]: value
    });
  },
  // 删除观众
  removeAudience(e) {
    const index = e.currentTarget.dataset.index;
    if (this.data.userOrder.length <= 1) {
      return;
    }
    const newList = [...this.data.userOrder];
    newList.splice(index, 1);
    this.setData({
      userOrder: newList
    });
    this.calculateTotal();
  },

  submitOrder() {
    //console.log(this.data.userOrder)
    for(let i=0;i<this.data.userOrder.length;i++){
      this.data.userOrder[i].ticketId=this.data.exhibitionData.id
      this.data.userOrder[i].createTime=util.formatTime(new Date())
    }
    wx.setStorage({
      key:'tempOrder',
      data: this.data.userOrder,
    })
    wx.setStorage({
      key:'tempExhibitionData',
      data: this.data.exhibitionData,
    })
    wx.setStorage({
      key:'tempCost',
      data: this.data.totalCost,
    })
    exhibition.submitOrder(this.data.userOrder).then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
    wx.navigateTo({
      url: '/pages/order/exhiOrder'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.setData({
      exhibitionData: wx.getStorageSync('tempReservationData')
    })
    // 使用后立即清理
    wx.removeStorageSync('tempReservationData');

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})